package cn.itcast.zt.demo02;

import com.lmax.disruptor.RingBuffer;

/**
 * 模拟外部磁盘数据或者网络传输过来的数据，并进行发布，是一个事件源，每次都触发，自己受到调用
 * create by zhangtian on 2019/1/22
 */
public class SumEventProducer {
    private final RingBuffer<SingleSumEvent> ringBuffer ;

    public SumEventProducer(RingBuffer<SingleSumEvent> ringBuffer) {
        this.ringBuffer = ringBuffer ;
    }

    public void onData(long sum){
        long sequence = ringBuffer.next() ;
        try {
            SingleSumEvent singleSumEvent = ringBuffer.get(sequence) ;
            singleSumEvent.caculate(1,sum);
            singleSumEvent.setValue(singleSumEvent.getValue());
            singleSumEvent.setThread(Thread.currentThread().getName());
        }finally {
            ringBuffer.publish(sequence);// 发布事件
        }
    }
}
